package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.common.util.UUIDUtil;
import com.softer.wxzj.entity.FlowConfig;
import com.softer.wxzj.mapper.FlowConfigMapper;
import com.softer.wxzj.mapper.FlowInstanceMapper;
import com.softer.wxzj.mapper.FlowTypeMapper;
import com.softer.wxzj.service.FlowConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class FlowConfigServiceImpl implements FlowConfigService {

    @Autowired
    private FlowConfigMapper flowConfigMapper;
    @Autowired
    private FlowInstanceMapper flowInstanceMapper;
    @Autowired
    private FlowTypeMapper flowTypeMapper;

    @Override
    public CommonResult replace(List<FlowConfig> configList) {
        if (configList.isEmpty()) {
            return CommonResult.ResponseResult(0, "请添加数据在保存");
        }
        int num = NumberEnum.ZERO.getNum();
        //数据验证
        List<String> roleCodeList = new ArrayList<>();
        List<Integer> flowStepList = new ArrayList<>();
        for (FlowConfig config : configList) {
            roleCodeList.add(config.getRoleCode());
            flowStepList.add(config.getFlowStep());
        }
        long roleCodeListCount = roleCodeList.stream().distinct().count();
        long flowStepListCount = flowStepList.stream().distinct().count();
        if (roleCodeListCount < configList.size() || flowStepListCount < configList.size()) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "请确认角色和流程！");
        }
        for (FlowConfig config : configList) {
            config.setCreater(ShiroUserUtil.getUserName());
            if (StringUtils.isBlank(config.getId())) {
                config.setId(UUIDUtil.getUUID());
            }
            //检查在途业务
            int count = flowInstanceMapper.getCountByConfigId(config.getId());
            if (count > 0) {
                return new CommonResult(ShiroResultEnum.FAIL.getCode(), "请检查审核中业务");
            }
            num = flowConfigMapper.replace(config);
        }
        return CommonResult.ResponseResult(num);
    }

    @Override
    public int getOperFlagByFlowType(String flowType) {
        //1提交   2备案
        int operFlag = NumberEnum.ONE.getNum();
        //查询当前用户是否在审批流中
        String flowStep = flowConfigMapper.getFlowStepByOperNoAndFlowType(ShiroUserUtil.getUserName(), flowType);
        if (StringUtils.isNotBlank(flowStep)) {
            //查询是否存在下个节点
            int count = flowConfigMapper.getNextCountByFlowStep(Integer.parseInt(flowStep), flowType);
            if (count == 0) {
                operFlag = NumberEnum.TWO.getNum();
            }
        }
        return operFlag;
    }

    /**
     * 查询申请人
     *
     * @param businessId 业务ID
     * @param flowType   类型
     * @return 申请人
     */
    @Override
    public String getApplicant(String businessId, String flowType) {
        return flowConfigMapper.getApplicant(businessId, flowType);
    }

    @Override
    public CommonResult insert(FlowConfig flowConfig) {
        //检查在途业务
        int count = flowInstanceMapper.getCountByConfigId(flowConfig.getId());
        if (count > 0) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "请检查审核中业务");
        }
        //检查是否符合新增条件
        int update = flowConfigMapper.isQualifiedCheck(flowConfig);
        if (update > 0) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "请确认流程顺序");
        }
        int i = flowConfigMapper.insert(flowConfig);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult update(FlowConfig flowConfig) {
        //检查在途业务
        int count = flowInstanceMapper.getCountByConfigId(flowConfig.getId());
        if (count > 0) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "请检查审核中业务");
        }
        //检查是否符合更新条件
        int update = flowConfigMapper.isQualifiedCheck(flowConfig);
        if (update > 1) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "请确认流程顺序");
        }
        int i = flowConfigMapper.update(flowConfig);
        return CommonResult.ResponseResult(i);
    }


    @Override
    public CommonResult delete(String id) {
        //检查在途业务
        int count = flowInstanceMapper.getCountByConfigId(id);
        if (count > 0) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "请检查审核中业务");
        }
        int i = flowConfigMapper.delete(id);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public PageInfo<FlowConfig> getList(Page page, FlowConfig flowConfig) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<FlowConfig> list = flowConfigMapper.getList(flowConfig);
        for (FlowConfig f : list) {
            String name = flowTypeMapper.getFlowNameByFlowType(f.getFlowType());
            f.setFlowTypeName(name);
        }
        return new PageInfo<>(list);
    }

    @Override
    public FlowConfig getInfo(String id) {
        return null;
    }

    @Override
    public List<FlowConfig> getConfigListByFlowType(String flowType) {
        return flowConfigMapper.getConfigListByFlowType(flowType);
    }

    @Override
    public FlowConfig getCurrentConfigByBusinessId(String businessId, String flowType) {
        FlowConfig config = flowConfigMapper.getCurrentConfigByBusinessId(businessId, flowType);
        //状态转换  1审批中   2审批通过  3审批不通过
        if (null != config) {
            if (String.valueOf(NumberEnum.ONE.getNum()).equals(config.getState())) {
                config.setState(config.getFlowState());
            } else if (String.valueOf(NumberEnum.ZERO.getNum()).equals(config.getState())) {
                config.setState(String.valueOf(NumberEnum.ONE.getNum()));
            }
        }
        return config;
    }
}
